package net.lzzy.practices.web.courses;

import io.swagger.annotations.Api;
import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.services.ChapterService;
import net.lzzy.practices.services.CourseService;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.utils.StringUtil;
import net.lzzy.practices.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 1
 */
@Api(tags= "章节")
@RestController
@RequestMapping(ApiConstant.ROUTE_CHAPTER_ROOT)
@CrossOrigin
public class ChapterApiController {
    private final ChapterService service;
    private final UserService uService;
    private final CourseService cService;

    @Autowired
    public ChapterApiController(ChapterService service, UserService uService, CourseService cService) {
        this.service = service;
        this.uService = uService;
        this.cService = cService;
    }

    @GetMapping(ApiConstant.ROUTE_CHAPTER_TEACHER)
    public ApiResult getTeachersChapters(@RequestParam String token, @RequestParam(name = "id") UUID courseId) {
        List<VmChapter> chapters = new ArrayList<>();
        service.getTeachersChapters(courseId).forEach(chapter -> chapters.add(VmChapter.create(chapter)));
        return new ApiResult(true, chapters.size() + "", chapters);

    }

    @GetMapping(ApiConstant.ROUTE_CHAPTER_STUDENT)
    public ApiResult getStudentsChapters(@RequestParam String token, @RequestParam(name = "id") UUID courseId) {
        List<VmChapter> chapters = new ArrayList<>();
        service.getStudentsChapters(courseId).forEach(chapter -> chapters.add(VmChapter.create(chapter)));
        return new ApiResult(true, chapters.size() + "", chapters);
    }

    public ApiResult addAndUpdate(UUID id, ChapterInfo info) {
        String token = info.getToken();
        String name = info.getName();
        String desc = info.getDesc();

        String courseId = info.getCid();
        boolean open = info.isOpen();


        if (StringUtil.isEmpty(name) ||
                StringUtil.isEmpty(desc) ||

                StringUtil.isEmpty(courseId)) {
            return new ApiResult(false, Messages.MISS_ARGS.toString(), null);
        }
        Course course = cService.getById(UUID.fromString(courseId));
        if (course == null) {
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }


        Chapter chapter;
        boolean add = id == null;
        if (add) {
            chapter = new Chapter();
            chapter.setCourse(course);
        } else {
            chapter = service.getById(id);
            if (chapter == null) {
                return new ApiResult(false, Messages.WRONG_ID.toString(), null);
            }
        }


        chapter.setName(name);
        chapter.setDescription(desc);
        chapter.setOpen(open);
        chapter.setUpdateTime(new Date());
        if (add) {
            return new ApiResult(true, Messages.SUCCESS.toString(), VmChapter.create(service.add(chapter)));
        } else {
            Chapter updated = service.update(chapter, token);
            if (updated == null) {
                return new ApiResult(false, Messages.WRONG_ID.toString(), null);
            } else {
                return new ApiResult(true, Messages.SUCCESS.toString(), VmChapter.create(updated));
            }
        }
    }


    @PostMapping(ApiConstant.ROUTE_CHAPTER_ADD)
    public ApiResult add(@RequestBody ChapterInfo info) {
        return addAndUpdate(null, info);
    }


    @PostMapping(ApiConstant.ROUTE_CHAPTER_PUT)
    public ApiResult update(@RequestBody ChapterInfo info, @PathVariable UUID id) {
        return addAndUpdate(id, info);
    }

    @PostMapping(ApiConstant.ROUTE_CHAPTER_OPEN_CLOSE)
    public ApiResult openCloseChapter(@RequestBody ChapterOpenInfo info) {
        String token = info.getToken();
        String strChapterId = info.getCid();
        boolean open = info.isOpen();
        if (StringUtil.isEmpty(token) || StringUtil.isEmpty(strChapterId)) {
            return new ApiResult(false, Messages.MISS_ARGS.toString(), false);
        }
        Chapter chapter = service.openCloseChapter(UUID.fromString(strChapterId), open, token);
        if (chapter == null) {
            return new ApiResult(false, Messages.NO_PERMISSION.toString(), false);
        }
        return new ApiResult(true, Messages.SUCCESS.toString(), chapter);
    }


    @DeleteMapping(ApiConstant.ROUTE_CHAPTER_REMOVE)
    public ApiResult remove(@RequestBody ChapterKeyInfo info) {
        String token = info.getToken();
        String strChapterId = info.getCid();
        String strCourseId = info.getCourseId();
        if (StringUtil.isEmpty(token) || StringUtil.isEmpty(strChapterId) || StringUtil.isEmpty(strCourseId)) {
            return new ApiResult(false, Messages.MISS_ARGS.toString(), null);
        }
        try {
            UUID courseId = UUID.fromString(strCourseId);
            UUID chapterId = UUID.fromString(strChapterId);
            Messages msg = service.remove(chapterId, courseId, token);
            return new ApiResult(msg == Messages.SUCCESS, msg.toString(), null);
        } catch (Exception e) {
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
    }
}